import Vue from 'vue'
import Router from 'vue-router'
import LoginPage from "../views/LoginPage";
import HomePage from "../views/HomePage";
import UserPage from "../views/user/UserPage";
import AddUserPage from "../views/user/AddUserPage";
import ModifyPasswordPage from "../views/user/ModifyPasswordPage";
import ServicePage from "../views/service/ServicePage";
import AddServicePage from "../views/service/AddServicePage";
import GoodsPage from "../views/goods/GoodsPage";
import AddGoodsPage from "../views/goods/AddGoodsPage";
import OrderPage from "../views/order/OrderPage";
import AddOrderPage from "../views/order/AddOrderPage";
import MerchantPage from "../views/merchant/MerchantPage";
import AddMerchantPage from "../views/merchant/AddMerchantPage";
import ModifyInfoPage from "../views/user/ModifyInfoPage";
import GoodsUseRecordPage from "../views/goods/GoodsUseRecordPage";

Vue.use(Router);

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'Login',
      component: LoginPage,
      meta: {requireAuth: false},
    },
    {
      path: '/home',
      component: HomePage,
      requireAuth: true,
      children: [
        {
          path: '/home/user',
          component: UserPage
        },
        {
          path: '/home/user/add',
          component: AddUserPage
        },
        {
          path: '/home/user/password',
          component: ModifyPasswordPage
        },
        {
          path: '/home/user/info',
          name:'ModifyInfoPage',
          component: ModifyInfoPage
        },
        {
          path: '/home/service',
          component: ServicePage
        },
        {
          path: '/home/service/add',
          component: AddServicePage
        },
        {
          path: '/home/goods',
          component: GoodsPage
        },
        {
          path: '/home/goods/add',
          component: AddGoodsPage
        },
        {
          path: '/home/goods/goodsUseRecord',
          component: GoodsUseRecordPage
        },
        {
          path: '/home/order',
          component: OrderPage
        },
        {
          path: '/home/order/add',
          component: AddOrderPage
        },
        {
          path: '/home/merchant',
          component: MerchantPage
        },
        {
          path: '/home/merchant/add',
          component: AddMerchantPage
        },
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断跳转目的路由是否需要登录
    let userJson = sessionStorage.getItem("user")
    if (!userJson) {
      next({
        path: '/login'
      })
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router


